package com.example.myblog1_0.controller;

import com.example.myblog1_0.model.LeaveMessage;
import com.example.myblog1_0.model.MyUser;
import com.example.myblog1_0.service.LeaveMessageService;
import com.example.myblog1_0.service.UserService;
import net.sf.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.security.Principal;

/**
 * @Author: xc
 * @Date: 2018/12/22 14:44
 * @Description:
 **/
@RestController
public class LeaveMessageController {
    @Autowired
    LeaveMessageService leaveMessageService;
    @Autowired
    UserService userService;

    @PostMapping("/saveMessage")
    public JSONObject saveMessage(LeaveMessage message,
                                    @AuthenticationPrincipal Principal principal){
        String username = principal.getName();
        MyUser user = userService.findUserInfoByName(username);
        message.setUsername(user.getNickname());
        message.setUserAvatarUrl(user.getAvatarUrl());
        return leaveMessageService.saveMessage(message);
    }

    @GetMapping("/findAllMessage")
    public JSONObject findAllMessage(@RequestParam("row") String row, @RequestParam("pageNum") String pageNum){
        return leaveMessageService.findAllMessage(Integer.parseInt(row),Integer.parseInt(pageNum));
    }
}
